﻿#include "SliderPropertyEditEntry.h"

#include <QSlider>
#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(Slider)

#include "EnumEditEntry.h"
#include "IntegerEditEntry.h"

struct SliderPropertyEditEntry::SliderPropertyEditEntryPrivate {
    QPointer<QSlider> object;

    QPointer<EnumEditEntry> entryTickPosition;
    QPointer<IntegerEditEntry> entryTickInterval;
};

SliderPropertyEditEntry::~SliderPropertyEditEntry() { }

SliderPropertyEditEntry::SliderPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new SliderPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void SliderPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QSlider*>(object);
}

void SliderPropertyEditEntry::onTick() {
    if(!p->object || isVisible()) {
        return ;
    }

    p->entryTickPosition->setValue(p->object->tickPosition());
    p->entryTickInterval->setValue(p->object->tickInterval());
}

void SliderPropertyEditEntry::initUi() {
    p->entryTickPosition = new EnumEditEntry;
    appendChildEntry(p->entryTickPosition);
    p->entryTickPosition->setMetaEnum(QMetaEnum::fromType<QSlider::TickPosition>());
    connect(p->entryTickPosition.data(), &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setTickPosition(QSlider::TickPosition(value));
    });

    p->entryTickInterval = new IntegerEditEntry;
    appendChildEntry(p->entryTickInterval);
    connect(p->entryTickInterval.data(), &IntegerEditEntry::valueChanged, [=](int value) {
        p->object->setTickInterval(value);
    });
}

void SliderPropertyEditEntry::retranslateUi() {
    setTitle(tr("QSlider"));
    p->entryTickPosition->setTitle(tr("tickPosition"));
    p->entryTickInterval->setTitle(tr("tickInterval"));
}
